{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creating the Json file"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Considering that synthetic random masks were created for data augmentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from os import listdir\n",
    "from os.path import join\n",
    "import shutil\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_dataset_path = \"../data_creator/New_DataSet\" # Set to none if no synthetic masks were created\n",
    "original_dataset_path = \"../../DataSet/ASNR-MICCAI-BraTS2023-Local-Synthesis-Challenge-Training\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "training_L = []\n",
    "\n",
    "for case in listdir(original_dataset_path):\n",
    "    entry_case = {\n",
    "        \"mask_healthy\": f\"{original_dataset_path}/{case}/{case}-mask-healthy.nii.gz\",\n",
    "        \"mask_unhealthy\": f\"{original_dataset_path}/{case}/{case}-mask-unhealthy.nii.gz\",\n",
    "        \"mask\": f\"{original_dataset_path}/{case}/{case}-mask.nii.gz\",\n",
    "        \"t1n_voided\": f\"{original_dataset_path}/{case}/{case}-t1n-voided.nii.gz\",\n",
    "        \"t1n\": f\"{original_dataset_path}/{case}/{case}-t1n.nii.gz\"\n",
    "        }\n",
    "    training_L.append(entry_case)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "if new_dataset_path!=None:\n",
    "    for case in listdir(new_dataset_path):\n",
    "        if \"-t1n-voided\" in case:\n",
    "            origin_name = case.split(\"-t1n-voided\")[0]\n",
    "            entry_case = {\n",
    "                \"t1n_voided\": f\"{new_dataset_path}/{case}\",\n",
    "                \"mask_healthy\": f\"{new_dataset_path}/{case.replace('-t1n-voided', '-mask-healthy')}\",\n",
    "                \"t1n\": f\"{original_dataset_path}/{origin_name}/{origin_name}-t1n.nii.gz\",\n",
    "                \"mask_unhealthy\":  f\"{original_dataset_path}/{origin_name}/{origin_name}-mask-unhealthy.nii.gz\"\n",
    "                }\n",
    "            training_L.append(entry_case)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Open the file for writing in text mode ('w')\n",
    "if new_dataset_path!=None:\n",
    "    json_path = f\"./augmented_{original_dataset_path.split('/')[-1]}.json\"\n",
    "else:\n",
    "    json_path = f\"./{original_dataset_path.split('/')[-1]}.json\"\n",
    "with open(json_path, 'w') as json_file:\n",
    "    json.dump({\"training\": training_L}, json_file, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "brats2023",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
